<?php 

/*
 * 个人中心管理控制器
 * */
class MemberController extends CommonController{
	/*
	 * 首页
	 * */
	 public function index(){
	 	//头部
		$this->topCate();
		//昨天个人信息，从公共部分调过来的
		$this->memberLogin();
		//通过get传参过来
		$uid=(int)$_GET['uid'];
		//统计我的提问
		if($uid){
			//统计提问总数	
			$myAsk=M()->query("SELECT count(*) as c FROM hd_ask WHERE uid={$uid}");
			$this->assign("myAsk",$myAsk);
			//我的提问
			$allAsk=M()->query("SELECT a.content,c.title,a.answer,a.time FROM hd_user as u JOIN hd_ask as a ON u.uid=a.uid JOIN hd_category as c ON a.cid=c.cid WHERE a.uid={$uid}");
			//p($allAsk);
			$this->assign("allAsk",$allAsk);
		}else{
			return;
		}
		
		//查询用户，回答表，问题表，分类关联起来查询回答的内容，分类，回答的时候，我的回答数
		$ansData=M()->query("SELECT an.content,c.title,an.time,a.answer FROM hd_user as u JOIN hd_answer as an ON u.uid=an.uid JOIN hd_ask as a ON an.asid=a.asid JOIN hd_category as c ON a.cid=c.cid WHERE an.uid={$uid}");
		//p($ansData);die;
		//分配出去
		$this->assign('ansData',$ansData);

	 	$this->display();
		
	 }
	/**
	 * 我的提问
	 */
	public function myAsk(){
		//头部
		$this->topCate();
		//左侧用户信息公共
		$this->memberLogin();
		//关联问题表和分类表
		//p($myAskData);
		//待解决和已解决判断 
		$w=isset($_GET['w']) ? $_GET['w'] : 0;
		$uid=$_GET['uid'];
		switch ($w) {
			case 0:
				$where="where a.uid={$uid} and solve=0";
				break;
			case 1:
				$where="where a.uid={$uid} and solve=1";
				break;
		}
		$sql="SELECT content,time,a.answer,title FROM hd_ask as a JOIN hd_category as c ON a.cid=c.cid  {$where} LIMIT 3";
		$myAskData=M()->query($sql);
//		p($myAskData);die;
		$this->assign('myAskData',$myAskData);
		
		
       $this->display(); 
    }
	/*
	 * 我的回答
	 * */
	public function myAnswer(){
		$this->topCate();
		$this->memberLogin();
		$w=isset($_GET['w']) ? $_GET['w'] : 0;
		//判断
		$uid=$_GET['uid'];
//		p($uid);
		switch ($w) {
			case '0':
				$where="where an.uid={$uid}";
				break;
			case '1':
				$where="where an.uid={$uid} and accept=1";
				break;
		}
		//p($where);
//		$sql="SELECT s.content,title,a.answer,s.time FROM hd_ask AS a JOIN hd_category as c ON a.cid=c.cid JOIN hd_answer as s ON a.asid=s.asid {$where}";
		$sql="SELECT distinct(a.asid),a.content,a.answer,c.title,a.time FROM `hd_answer` as an inner join hd_ask as a on an.asid=a.asid  inner join hd_category as c on  a.cid=c.cid {$where}";

		$myAnswer=M()->query($sql);
		//p($myAnswer);die;
		$this->assign('myAnswer',$myAnswer);
        $this->display(); 
    }
	/*
	 * 我的等级
	 * */
	public function myLv(){
		//头部
		$this->topCate();
		$this->memberLogin();
		//循环所有等级
		$myLv=include "./Application/Common/conf/lvConfig.php";
		//p($myLv);
		$this->assign('myLv',$myLv);
		//获得用户自己的等级
		$uid=$_GET['uid'];
		$myLev=M()->query("SELECT * FROM hd_user WHERE uid={$uid}");
		$myLev[0]['lv']=$this->expToLv($myLev[0]['exp']);
		//他的经验等于历史经验加一 lvToExp是在公共写的一个放法，是等级转换经验；
		$exp=$this->lvToExp($myLev[0]['lv']+1);
		$gapExp=$exp-$myLev[0]['exp'];
		//分配相差经验变量
		$this->assign('gapExp',$gapExp);
		$this->assign('myLev',$myLev);
      	$this->display(); 
    }
	/*
	 * 我的金币
	 * */
	public function myGold(){
		$this->topCate();
		$this->memberLogin();
		
       $this->display(); 
    }
	//我的头像，需要把上传类融入框架
	public function myFace(){
		$this->topCate();
		$this->memberLogin();
		if(IS_POST){			
			$uid=$_SESSION['uid'];
			$sql="SELECT * FROM hd_user WHERE uid={$uid}";
			$facePath=M()->query($sql);
			if(!empty($facePath[0]['face'])){
//				移除头像图片（face图片）
				unlink($facePath[0]['face']);
			}
//			p($path);			
			$upload=new Upload();
			$path=$upload->up();
			$sql="update hd_user SET face='{$path[0]}' WHERE uid={$uid}";
			M()->exec($sql);
						
			$this->assign('face',$path);
			$this->error('上传成功');
		}
		
    	$this->display(); 
    }
	
}













 ?>